package com.tuanzi.loan.business.repository.family.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.family.LoanCreditCardInfo;
import com.tuanzi.loan.business.repository.family.custom.LoanCreditCardInfoRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.credit.LoanCreditCardInfoRequest;
import org.apache.commons.lang3.StringUtils;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCreditCardInfo.LOAN_CREDIT_CARD_INFO;

/**
 * User: liangZongXi
 * Create time: 19:47 2018/8/30
 * Description:
 */
@Repository
public class LoanCreditCardInfoRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanCreditCardInfoRepositoryCustom {
    @Override
    public Page<LoanCreditCardInfo> list(LoanCreditCardInfoRequest searchRequest, Pageable pageable) {

        SelectConditionStep<Record> sql = dslContext.select(LOAN_CREDIT_CARD_INFO.fields()).from(LOAN_CREDIT_CARD_INFO)
                .where(DSL.trueCondition());

        if(null != searchRequest.getPlatform()){
            ArrayList<Integer> platforms = Lists.newArrayList();
            platforms.add(0);

            if(0 != searchRequest.getPlatform()){
                platforms.add(searchRequest.getPlatform());
            }
            sql.and(LOAN_CREDIT_CARD_INFO.PLATFORM.in(platforms));
        }
        if(StringUtils.isNotBlank(searchRequest.getPrdid())){
            sql.and(LOAN_CREDIT_CARD_INFO.PRDID.eq("0"));

            if(!"0".equals(searchRequest.getPrdid())){
                sql.or(LOAN_CREDIT_CARD_INFO.PRDID.startsWith(searchRequest.getPrdid()));
            }

        }
        if(null != searchRequest.getTag()){
            sql.and(LOAN_CREDIT_CARD_INFO.TAG.contains(searchRequest.getTag()));
        }
        if(null != searchRequest.getStatus()){
            sql.and(LOAN_CREDIT_CARD_INFO.STATUS.eq(searchRequest.getStatus().shortValue()));
        }
        if(null != searchRequest.getAdvertSpace()){
            ArrayList<Integer> advertSpaces = Lists.newArrayList();
            advertSpaces.add(0);

            if(0 != searchRequest.getAdvertSpace()){
                advertSpaces.add(searchRequest.getAdvertSpace());
            }
            sql.and(LOAN_CREDIT_CARD_INFO.ADVERT_SPACE.in(advertSpaces));
        }
        SelectQuery<Record> query = sql.orderBy(LOAN_CREDIT_CARD_INFO.POSITION.asc()).getQuery();


        return search(query,pageable,record ->{
            LoanCreditCardInfo creditCardInfo = new LoanCreditCardInfo();
            creditCardInfo.setId(record.get(LOAN_CREDIT_CARD_INFO.ID));
            creditCardInfo.setPlatform(record.get(LOAN_CREDIT_CARD_INFO.PLATFORM));
            creditCardInfo.setPrdid(record.get(LOAN_CREDIT_CARD_INFO.PRDID));
            creditCardInfo.setChannel(record.get(LOAN_CREDIT_CARD_INFO.CHANNEL));
            creditCardInfo.setPosition(record.get(LOAN_CREDIT_CARD_INFO.POSITION));
            creditCardInfo.setStartTime(record.get(LOAN_CREDIT_CARD_INFO.STARTTIME));
            creditCardInfo.setEndTime(record.get(LOAN_CREDIT_CARD_INFO.ENDTIME));
            creditCardInfo.setUrl(record.get(LOAN_CREDIT_CARD_INFO.URL));
            creditCardInfo.setLogo(record.get(LOAN_CREDIT_CARD_INFO.LOGO));
            creditCardInfo.setName(record.get(LOAN_CREDIT_CARD_INFO.NAME));
            creditCardInfo.setSummary(record.get(LOAN_CREDIT_CARD_INFO.SUMMARY));
            creditCardInfo.setInitCount(record.get(LOAN_CREDIT_CARD_INFO.INITCOUNT));
            creditCardInfo.setSuccessCount(record.get(LOAN_CREDIT_CARD_INFO.SUCCESS_COUNT));
            creditCardInfo.setTag(record.get(LOAN_CREDIT_CARD_INFO.TAG));
            creditCardInfo.setStatus(record.get(LOAN_CREDIT_CARD_INFO.STATUS).intValue());
            creditCardInfo.setAdvertSpace(record.get(LOAN_CREDIT_CARD_INFO.ADVERT_SPACE));
            creditCardInfo.setShowState(record.get(LOAN_CREDIT_CARD_INFO.SHOW_STATE).intValue());
            creditCardInfo.setPrompt(record.get(LOAN_CREDIT_CARD_INFO.PROMPT));
            creditCardInfo.setOperator(record.get(LOAN_CREDIT_CARD_INFO.OPERATOR));
            creditCardInfo.setCTime(record.get(LOAN_CREDIT_CARD_INFO.CTIME));
            creditCardInfo.setSummaryAdd(record.get(LOAN_CREDIT_CARD_INFO.SUMMARY_ADD));

            return creditCardInfo;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_CREDIT_CARD_INFO;
    }
}
